﻿using Core.Dtos;
using Core.Domain;
using Core.IRepository;
using Core.IServices;
using Core.Messages;
using Core.Queries;
using Core.ViewModels;
using Library.Cache;
using Library.Helpers;
using Library.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Core.Mappers;
using Core.Aspects;
using Core.IRepository.Admin;

namespace Services
{
    [CacheName("Dictionary")]
    public class ReadDictionaryService : BaseService, IReadDictionaryService
    {
        private const string CACHE_DICTIONARIES = "Dictionaries";

        private readonly IAdminRepository<int, DictionaryItem> dictionaryItemRepository;        
        private readonly IAdminUnitOfWork unitOfWork;        

        public ReadDictionaryService(IAdminUnitOfWork unitOfWork, IAdminRepository<int, DictionaryItem> dictionaryItemRepository)
        {
            this.dictionaryItemRepository = dictionaryItemRepository;        
            this.unitOfWork = unitOfWork;        
        }

        public Response<IList<IDictionaryItem>> GetItems(DictionaryName dictionary)
        {
            return Run<IList<IDictionaryItem>>(() =>
            {
                return GetAllItems().FindBy(dictionary).ToList();
            });
        }

        [Cache(Name = CACHE_DICTIONARIES, NoExpiration = true)]
        public IEnumerable<IDictionaryItem> GetAllItems()
        {
            return dictionaryItemRepository.FindAll().Map<DictionaryItem, DictionaryItemDto>();
        }

        public void InitStaticCache()
        {
            GetAllItems();
        }

        [DeleteCache(Name = CACHE_DICTIONARIES)]
        public Response ResetStaticCache()
        {
            return Run(() =>
            {
                InitStaticCache();
            });
        }
    }
}
